//solution adapter
//$Id: solution.h 395 2010-03-26 14:01:03Z Oleg.Bulychov $
#pragma once

#include <list>
#include "src/heo/include/sys_api.h"

template<class A, class B>
struct solution;


template<class TSolution>
struct Solution_Pool
{
    std::list<TSolution> sol_list_;
    typedef typename std::list<TSolution>::iterator       iterator;
    typedef typename std::list<TSolution>::const_iterator const_iterator;

    void resize(size_t nSize)
    {
        ////size_t old = sol_list_.size();
        ////if (old < nSize)
        ////{
        ////    for (size_t i = 0; i < nSize - old; ++i)
        ////    {
        ////        sol_list_.push_back(TSolution());
        ////    }
        ////}
        ////else if (old > nSize)
        ////{
        ////    for (size_t i = 0; i < old - nSize; ++i)
        ////    {
        ////        sol_list_.pop_back();
        ////    }
        ////}
        sol_list_.resize(nSize, TSolution());
    }

    template<class TSolver>
    bool init(TSolver& solver)
    {
        typename TSolver::TEnvironment* env = solver.get_Environment();
        //uniform_random& rnd = solver.get_Rnd();
        if (env->initial_solution_.length() > 0)
        {
            //TODO read from file
            return TSolution::read(env, env->initial_solution_, *this);
        }
        else
        {
            ////for (size_t i = 0; i < sol_list_.size(); ++i)
            ////{
            ////    sol_list_[i].init(env, rnd);
            ////    sol_list_[i].generate(env, rnd);
            ////    sol_list_[i].evaluate(env, rnd);
            ////}
        }
        return true;
    }

    template<class TEnv>
    void output(TEnv* env)
    {
        size_t i = 0;
        size_t count = sol_list_.size();
        for (iterator it = sol_list_.begin(); it != sol_list_.end(); ++it, ++i)
        {
            if (1 == count)
                std::cout << "solution:" << lf;
            else
                std::cout << "solution[" << i << "]:" << lf;
            it->print(std::cout, env);
        }
        if (env->output_dir_.length())
        {
            std::string name = env->output_dir_;
            sys_api::Create_Dir_Recursive(env->output_dir_.c_str());
            sys_api::Path_Append(name, "solution.dat");
            size_t id = 0;
            for (iterator it = sol_list_.begin(); it != sol_list_.end(); ++it, ++id)
            {
                it->write(env, name, id, count);
            }
        }
    }
};

//EOF!
